<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>

      <el-col :span="20">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <!-- <el-form-item label="父部门id" prop="parentId">
            <el-input v-model="queryParams.parentId" placeholder="请输入父部门id" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <!-- <el-form-item label="祖级列表" prop="ancestors">
            <el-input v-model="queryParams.ancestors" placeholder="请输入祖级列表" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <el-form-item label="部门名称" prop="deptName">
            <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="显示顺序" prop="orderNum">
            <el-input v-model="queryParams.orderNum" placeholder="请输入显示顺序" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <!-- <el-form-item label="负责人" prop="leader">
            <el-input v-model="queryParams.leader" placeholder="请输入负责人" clearable @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <!-- <el-form-item label="联系电话" prop="phone">
            <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <!-- <el-form-item label="邮箱" prop="email">
            <el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <!-- <el-form-item label="是否生效：  0、已生效  1、待生效" prop="isValid">
            <el-input v-model="queryParams.isValid" placeholder="请输入是否生效：  0、已生效  1、待生效" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <el-form-item label="楼栋码" prop="buildingCode">
            <el-input v-model="queryParams.buildingCode" placeholder="请输入楼栋码" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="总楼层数" prop="floorNum">
            <el-input v-model="queryParams.floorNum" placeholder="请输入总楼层数" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="楼层" prop="floor">
            <el-input v-model="queryParams.floor" placeholder="请输入楼层" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['lock:buiding:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-if="refreshTable" v-loading="loading" :data="buidingList" row-key="deptId"
          :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column label="父部门id" prop="parentId" />
          <el-table-column label="祖级列表" align="center" prop="ancestors" />
          <el-table-column label="部门名称" align="center" prop="deptName" />
          <el-table-column label="显示顺序" align="center" prop="orderNum" />
          <el-table-column label="负责人" align="center" prop="leader" />
          <el-table-column label="联系电话" align="center" prop="phone" />
          <el-table-column label="邮箱" align="center" prop="email" />
          <el-table-column label="部门状态" align="center" prop="status" />
          <el-table-column label="类型" align="center" prop="type" />
          <el-table-column label="是否生效" align="center" prop="isValid" />
          <el-table-column label="楼栋码" align="center" prop="buildingCode" />
          <el-table-column label="总楼层数" align="center" prop="floorNum" />
          <el-table-column label="楼层" align="center" prop="floor" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['lock:buiding:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
                v-hasPermi="['lock:buiding:add']">新增</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['lock:buiding:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>


        <!-- 添加或修改建筑架构对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="父部门id" prop="parentId">
              <treeselect v-model="form.parentId" :options="buidingOptions" :normalizer="normalizer"
                placeholder="请选择父部门id" />
            </el-form-item>
            <el-form-item label="部门名称" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入部门名称" />
            </el-form-item>
            <el-form-item label="显示顺序" prop="orderNum">
              <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
            </el-form-item>
            <el-form-item label="负责人" prop="leader">
              <el-input v-model="form.leader" placeholder="请输入负责人" />
            </el-form-item>
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
            <el-form-item label="删除标志" prop="delFlag">
              <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
            </el-form-item>
            <el-form-item label="是否生效：  0、已生效  1、待生效" prop="isValid">
              <el-input v-model="form.isValid" placeholder="请输入是否生效：  0、已生效  1、待生效" />
            </el-form-item>
            <el-form-item label="楼栋码" prop="buildingCode">
              <el-input v-model="form.buildingCode" placeholder="请输入楼栋码" />
            </el-form-item>
            <el-form-item label="总楼层数" prop="floorNum">
              <el-input v-model="form.floorNum" placeholder="请输入总楼层数" />
            </el-form-item>
            <el-form-item label="楼层" prop="floor">
              <el-input v-model="form.floor" placeholder="请输入楼层" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>

  </el-col>

  </el-row>
  </div>
</template>

<script>
  import {
    listBuiding,
    getBuiding,
    delBuiding,
    addBuiding,
    updateBuiding,
    buidingTreeSelect
  } from "@/api/lock/buiding";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Buiding",
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 建筑架构表格数据
        buidingList: [],
        // 建筑架构树选项
        buidingOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否展开，默认全部展开
        isExpandAll: true,
        // 重新渲染表格状态
        refreshTable: true,
        // 部门名称
        deptName: undefined,
        // 建筑树选项
        deptOptions: undefined,
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 查询参数
        queryParams: {
          parentId: null,
          ancestors: null,
          deptName: null,
          orderNum: null,
          leader: null,
          phone: null,
          email: null,
          status: null,
          type: null,
          isValid: null,
          buildingCode: null,
          floorNum: null,
          floor: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getaBuidingTree();
    },
    methods: {
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.deptId = data.id;
        this.handleQuery();
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      /** 查询建筑下拉树结构 */
      getaBuidingTree() {
        buidingTreeSelect().then(response => {
          this.deptOptions = response.data;
        });
      },
      /** 查询建筑架构列表 */
      getList() {
        this.loading = true;
        listBuiding(this.queryParams).then(response => {
          this.buidingList = this.handleTree(response.data, "deptId", "parentId");
          this.loading = false;
        });
      },
      /** 转换建筑架构数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.deptId,
          label: node.deptName,
          children: node.children
        };
      },
      /** 查询建筑架构下拉树结构 */
      getTreeselect() {
        listBuiding().then(response => {
          this.buidingOptions = [];
          const data = {
            deptId: 0,
            deptName: '顶级节点',
            children: []
          };
          data.children = this.handleTree(response.data, "deptId", "parentId");
          this.buidingOptions.push(data);
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          deptId: null,
          parentId: null,
          ancestors: null,
          deptName: null,
          orderNum: null,
          leader: null,
          phone: null,
          email: null,
          status: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          type: null,
          deleteFlg: null,
          isValid: null,
          buildingCode: null,
          floorNum: null,
          floor: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.getTreeselect();
        if (row != null && row.deptId) {
          this.form.parentId = row.deptId;
        } else {
          this.form.parentId = 0;
        }
        this.open = true;
        this.title = "添加建筑架构";
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getTreeselect();
        if (row != null) {
          this.form.parentId = row.parentId;
        }
        getBuiding(row.deptId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改建筑架构";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.deptId != null) {
              updateBuiding(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBuiding(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除建筑架构编号为"' + row.deptId + '"的数据项？').then(function() {
          return delBuiding(row.deptId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
</script>
